package com.ruoyi.project.lesson.order.service.impl;

import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.lesson.lesson.domain.Lesson;
import com.ruoyi.project.lesson.lesson.mapper.LessonMapper;
import com.ruoyi.project.lesson.order.domain.LessonOrder;
import com.ruoyi.project.lesson.order.mapper.LessonOrderMapper;
import com.ruoyi.project.lesson.order.service.ILessonOrderService;
import net.ipip.ipdb.CityInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程订单Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-12-07
 */
@Service
@DataSource(DataSourceType.KKFORMULA)
public class LessonOrderServiceImpl implements ILessonOrderService {

    @Autowired
    private LessonOrderMapper lessonOrderMapper;

    @Autowired
    private LessonMapper lessonMapper;

    /**
     * 查询课程订单
     *
     * @param id 课程订单ID
     * @return 课程订单
     */
    @Override
    public LessonOrder selectLessonOrderById(Long id) {
        return lessonOrderMapper.selectLessonOrderById(id);
    }

    /**
     * 查询课程订单列表
     *
     * @param lessonOrder 课程订单
     * @return 课程订单
     */
    @Override
    public List<LessonOrder> selectLessonOrderList(LessonOrder lessonOrder) {
        List<LessonOrder> orders = lessonOrderMapper.selectLessonOrderList(lessonOrder);
        orders.forEach(order -> {
            CityInfo cityInfo = IpUtils.getCityInfo(order.getIp());
            if (cityInfo != null) {
                order.setIpInfo(cityInfo.getRegionName() + " " + cityInfo.getCityName());
            }
            Lesson lesson = lessonMapper.selectLessonById(order.getLessonId());
            if (lesson != null) {
                order.setLessonName(lesson.getName());
            }
        });
        return orders;
    }

    /**
     * 新增课程订单
     *
     * @param lessonOrder 课程订单
     * @return 结果
     */
    @Override
    public int insertLessonOrder(LessonOrder lessonOrder) {
        return lessonOrderMapper.insertLessonOrder(lessonOrder);
    }

    /**
     * 修改课程订单
     *
     * @param lessonOrder 课程订单
     * @return 结果
     */
    @Override
    public int updateLessonOrder(LessonOrder lessonOrder) {
        return lessonOrderMapper.updateLessonOrder(lessonOrder);
    }

    /**
     * 删除课程订单对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLessonOrderByIds(String ids) {
        return lessonOrderMapper.deleteLessonOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程订单信息
     *
     * @param id 课程订单ID
     * @return 结果
     */
    @Override
    public int deleteLessonOrderById(Long id) {
        return lessonOrderMapper.deleteLessonOrderById(id);
    }
}
